<template>
  <div id="phone">
    <div class="img">
      <img src="https://img1.baidu.com/it/u=4152366307,3892585726&fm=253&fmt=auto&app=138&f=JPEG?w=987&h=800"
        class="tx" />
      <div class="login">
        <div class="box">
          <el-form ref="form" :rules="formRules" :model="formModel">
            <el-form-item prop="phone">
              <el-input v-model="formModel.phone" placeholder="请输入手机号" clearable />
            </el-form-item>
            <el-form-item>
              <el-input v-model="formModel.yzm" placeholder="请输入验证码" clearable style="width: 65%;" />
              <button
                style="width: 30%; margin-left: 5px; height: 30px; background-color: #67C23A; color: #fff; border: none; border-radius: 5px;"
                @click="sendCode" :disabled="flag" type="button">
                {{ flag ? `倒计时${num}` : '发送验证码' }}
              </button>
            </el-form-item>
            <el-button type="primary" @click="loginphone">登录</el-button>
            <el-button type="info" @click="router.push('/login')">用户名登录</el-button>
          </el-form>

        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ElMessage } from "element-plus";
import { useRouter } from 'vue-router'
import axios from "axios";
import { reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { log } from "console";
let flag = ref(false);
let num = ref(60);
let router = useRouter()
const formModel = reactive({
  phone: "",
  yzm: ""
});
let formRules = reactive({
  phone: [
    { required: true, message: "请输入手机号" },
    { min: 11, max: 11, message: "手机号长度需要在11位" },
  ]
});
const startCountdown = () => {
  flag.value = true;
  num.value = 60;
  const interval = setInterval(() => {
    if (num.value > 0) {
      num.value--;
    } else {
      clearInterval(interval);
      flag.value = false;
    }
  }, 1000);
};
let sendCode = async () => {
  let res = await axios.get("http://localhost:3000/sendCode?phone=" + formModel.phone)
  if (res.data.code == 200) {
    startCountdown()
    ElMessage.success("发送成功")
  } else {
    ElMessage.error("发送失败")
  }
}
let loginphone = async () => {
  try {
    let res = await axios.post("http://localhost:3000/loginphone", {
      phone: formModel.phone,
      inputCode: formModel.yzm
    });

    if (res.data.code === 200) {
      ElMessage.success("登录成功");
      // 存储双 token
      localStorage.setItem("accessToken", res.data.accessToken);
      localStorage.setItem("refreshToken", res.data.refreshToken);
      localStorage.setItem("name", res.data.name);
      // 跳转到主页或其他页面
      router.push('/home');
    } else {
      ElMessage.error(res.data.msg);
    }
  } catch (error) {
    ElMessage.error("登录失败");
  }
};
</script>

<style scoped lang="scss">
#phone {
  width: 100%;
  height: 100%;
  background: linear-gradient(to right top, #ecf5ff, #c6e2ff);

  .img {
    width: 100%;
    height: 100vh;
    background-image: url("https://img1.baidu.com/it/u=3743680466,2149120321&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=500");
    background-size: cover;
    background-position: center;
  }

  .tx {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    position: absolute;
    top: 34%; // 垂直居中
    left: 50%; // 水平居中
    transform: translate(-50%, -30%); // 通过 translate 实现精确居中
    z-index: 1;
  }

  .login {
    width: 500px;
    height: 300px;
    background-color: rgba($color: #000000, $alpha: 0.5);
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;

    .box {
      width: 50%;
      height: 100px;
      margin: 0 auto;
      margin-top: 80px;
    }
  }
}
</style>